<?php
class IndexController extends My_Controllers {
	public function indexAction() {
		$this->_helper->layout->setLayout ( 'index' );
		$link = array ('rel' => 'pingback', 'href' => 'http://www.zy2f.com/index.php/xmlrpc' );
		$this->view->headLink ( $link, 'PREPEND' );
		$this->view->headMeta ()->appendName ( 'keywords', 'framework php productivity' );
		
		if ($this->_request->getBaseUrl () == '') {
			$this->_redirect ( '/index.php' );
		}
		
		$select = new Zend_Db_Select ( $this->db );
		$select->from ( array ('n' => 'bc_news' ), array ('id', 'news_title', 'time', 'news_view', 'column_id', 'section_id' ) );
		$select->joinLeft ( array ('l' => 'bc_lanmu' ), 'n.column_id = l.id', array ('lname' => 'name' ) );
		$select->joinLeft ( array ('k' => 'bc_keshi' ), 'l.kid = k.id', array ('kename' ) );
		//        $select->joinLeft ( array ('u' => 'bc_users' ), 'n.news_uid = u.id', array ('username' ) );
		$select->where ( 'mode = ?', '1' );
		$select->where ( 'l.name != ?', '' );
		//        $select->orWhere ( 'leibie_id = ?', 2 );
		$select->limitPage ( 0, 16 );
		$select->order ( "news_view" )->order ( "id desc" );
		$select->order ( "time" );
		$sql = $select->__toString ();
		//        Zend_Debug::dump ( $sql );
		$news_hot_arr = $this->db->fetchAll ( $sql );
		Zend_Debug::dump ( $news_hot_arr, '$news_hot_arr', false );
		$this->view->news_hot_list = $news_hot_arr;
		//		$this->view->news_hot_list = $this->news->fetchAll ( NULL, NULL, 7, 0 )->toArray ();
		$select_gun = new Zend_Db_Select ( $this->db );
		$select_gun->from ( array ('n' => 'bc_news' ), array ('id', 'news_title' ) );
		$select_gun->where ( 'mode = ?', '1' );
		$select_gun->order ( 'time DESC' );
		$sql_gun = $select_gun->__toString ();
		$this->view->gun = $this->db->fetchAll ( $sql_gun );
		//		Zend_Debug::dump ( $this->view->gun );
		

		$ke_array = $this->k ( null, 130, 0 );
		$arr = new My_Helper_Array ( );
		$ke_array = $arr->array_group_by ( $ke_array, 'leibie_id' );
		//		Zend_Debug::dump ( $ke_array );
		$this->view->ke_array_xingzheng = $ke_array [2];
		$this->view->ke_array_yiliao = $ke_array [1];
		$this->view->ke_array_houqin = $this->k ( 3, 130, 0 );
		$this->view->xingzheng = array_slice ( $ke_array [2], 0, 13 );
		$this->view->yiliao = array_slice ( $ke_array [1], 0, 13 );
		
		$select_l = $this->db->select ();
		$select_l->from ( array ('l' => 'bc_lanmu' ), array ('id', 'name' ) );
		$select_l->joinLeft ( array ('n' => 'bc_news' ), 'l.id = n.column_id', array ('article_amount' => 'count(n.id)' ) );
		$select_l->group ( 'l.id' );
		$select_l->order ( 'article_amount desc' );
		$select_l->limitPage ( 0, 10 );
		
		$sql = $select_l->__toString ();
		$item0 = $this->view->item0 = $this->k ( null, 4, 0 );
		$item1 = $this->view->item1 = $this->k ( null, 4, 4 );
		$this->view->headScript ()->appendFile ( '/html/scripts/easing.js' );
		$this->view->headScript ()->appendFile ( '/html/scripts/module/default/index.js' );
		
		$select_pic = new Zend_Db_Select ( $this->db );
		$select_pic->from ( 'bc_pics', array ('id' ) );
		$select_pic->limit ( 5, 0 );
		$select_pic->order ( 'id desc' );
		$this->view->item_pic = $this->db->fetchAll ( $select_pic );
		//        Zend_Debug::dump( $pic );
	}
	protected function k($k = null, $limit, $offset) {
		$select_k = $this->db->select ();
		$select_k->from ( array ('k' => 'bc_keshi' ), array ('id', 'kename', 'leibie_id' ) );
		$select_k->joinLeft ( array ('n' => 'bc_news' ), 'k.id = n.section_id', array ('article_amount' => 'count(n.id)' ) );
		$select_k->group ( 'k.id' );
		$select_k->limit ( $limit, $offset );
		$select_k->order ( 'article_amount desc' );
		$select_k->order ( 'id' );
		//		$select_k->where ( 'n.mode = ?', 1 );
		if ($k != '') {
			$select_k->where ( 'leibie_id = ?', $k );
		} else {
			$select_k->where ( 'leibie_id != ?', 3 );
		}
		$sql = $select_k->__toString ();
		//		Zend_Debug::dump ( $sql );
		return $this->db->fetchAll ( $sql );
	}
	public function sectionAction() {
		$this->_helper->layout->setLayout ( 'section' );
		
		$id = $this->_getParam ( 'id' );
		$keshi = new Keshi ( );
		$this->view->keshiDb = $keshi->find ( $id )->current ();
		$lanmu = new Lanmu ( );
		$rows_lanmu = $lanmu->fetchAll ( "kid = $id" )->toArray ();
		
		$article = new Article ( );
		foreach ( $rows_lanmu as $k => $v ) {
			$s = $article->select ();
			$s->where ( 'column_id = ?', $v ['id'] );
			$s->where ( 'mode > ?', - 1 );
			$sql = $s->__toString ();
			//            Zend_Debug::dump ( $sql );
			$rows_lanmu [$k] ['news'] = $article->fetchAll ( $s )->toArray ();
		}
		$this->view->lanmuDb = $rows_lanmu;
		
		$db = Zend_Registry::get ( 'db' );
		$select = $db->select ();
		$select->from ( array ('n' => 'bc_news' ), array ('id', 'news_title', 'time', 'news_view', 'column_id', 'section_id' ) );
		$select->joinLeft ( array ('l' => 'bc_lanmu' ), 'n.column_id = l.id', array ('lname' => 'name' ) );
		$select->joinLeft ( array ('k' => 'bc_keshi' ), 'n.section_id = k.id', array ('kename' ) );
		$select->joinLeft ( array ('u' => 'bc_users' ), 'n.news_uid = u.id', array ('username' ) );
		$select->where ( 'mode > ?', 0 );
		$select->where ( 'section_id = ?', $id );
		$select->limitPage ( 0, 10 );
		$select->order ( "news_view desc" );
		$select->order ( "id desc" );
		$sql = $select->__toString ();
		$this->view->news_hot_arr = $db->fetchAll ( $sql );
		Zend_Debug::dump ( $this->view->news_hot_arr, '', false );
		
		$pic = new Pic ( );
		$select_pic = $pic->select ();
		$select_pic->limitPage ( 0, 3 );
		$select_pic->order ( 'id DESC' );
		$this->view->rows_pic = $pic->fetchAll ( $select_pic );
		//		Zend_Debug::dump ( $rows_pic );
	}
	public function tAction() {
		//		echo 'ss';
	//		$select = $this->db->select ();
	//		$select->from ( array ('n' => 'bc_news' ), array ('id', 'time' ) );
	//		$select->where ( 'mode = ?', '2' );
	//		$sql = $select->__toString ();
	//		Zend_Debug::dump ( $sql );
	//		$r = $this->db->fetchAll ( $select );
	//
	//		foreach ( $r as $v ) {
	//			$set = array ('mode' => '1' ); //
	//			$table = 'bc_news'; //
	//			$where = $this->db->quoteInto ( 'id = ?', $v ['id'] ); //
	//			$rows_affected = $this->db->update ( $table, $set, $where );
	//			echo $rows_affected;
	//		}
	//		Zend_Debug::dump ( $r );
	}
	function str_format_time($timestamp = '') {
		if (preg_match ( "/[0-9]{4}-[0-9]{1,2}-[0-9]{1,2} (0[0-9]|1[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])/i", $timestamp )) {
			list ( $date, $time ) = explode ( " ", $timestamp );
			list ( $year, $month, $day ) = explode ( "-", $date );
			list ( $hour, $minute, $seconds ) = explode ( ":", $time );
			$timestamp = gmmktime ( $hour, $minute, $seconds, $month, $day, $year );
		} else {
			$timestamp = time ();
		}
		return $timestamp;
	}
	public function barAction() {
		$mail = new Zend_Mail ( );
		$mail->setBodyText ( 'This is the text of the mail.' );
		$mail->setFrom ( '2012728@qq.com', 'Some Sender' );
		$mail->addTo ( '2012728@qq.com', 'Some Recipient' );
		$mail->setSubject ( 'TestSubject' );
		$mail->send ();
		$this->front->setParam ( 'noViewRenderer', true );
	}
	public function phpAction() {
		$this->front->setParam ( 'noViewRenderer', true );
		echo phpinfo ();
	}
}
